/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.panomedic.gui;

import com.panomedic.core.LUT;
import com.panomedic.log4j.Logger;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

/**
 *
 * @author Yare
 */
class LUTPanel extends JPanel {
    
    static Logger logger = new Logger();

    LUT lut = null;
    /**
     * Point where the graph starts
     */
    public static final Point offset = new Point(60, 10);
    /**
     * Dimension of the LUT
     */
    public static final int graphSize = 300;
    /**
     * Component
     */
    public int component;
    public static final int secAxesCount = 5;
    public static final boolean showSecAxes = true;

    public LUTPanel(LUT lut, int component) {
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
        this.lut = lut;
        this.component = component;
    }

    public Dimension getSize() {
        return new Dimension(graphSize + offset.x + offset.y, graphSize + offset.y + offset.x);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;

        AffineTransform oldat = g2.getTransform();
        AffineTransform ct = AffineTransform.getTranslateInstance(offset.x, offset.y);
        g2.transform(ct);

        g2.setColor(Color.WHITE);
        g2.fillRect(0, 0, graphSize, graphSize);

        //  secondary axes
        if (showSecAxes) {
            g2.setColor(Color.LIGHT_GRAY);
            double interval = ((double) graphSize / secAxesCount);
            for (int i = 0; i < secAxesCount; i++) {
                g2.drawLine((int) ((i + 1) * interval), 0, (int) ((i + 1) * interval), graphSize);
                g2.drawLine(0, (int) (i * interval), graphSize, (int) (i * interval));
            }
        }
        //  primary axes
        g2.setColor(Color.BLACK);
        g2.drawLine(0, graphSize, graphSize, graphSize);
        g2.drawLine(0, graphSize, 0, 0);


        //  drawing the actual LUT
        if (lut != null) {
            int size = lut.getRange()[component];// * lut.getAccuracy();
            double dx = (double) size / graphSize;
            int lastX = 0;
            int lastY1 = 0;
            int lastY2 = 0;

            for (int x = 1; x <= graphSize; x++) {
                double lutX = x * dx;
                double lutY1 = lut.getMapping(lutX, component, lut.getP1());
                double lutY2 = lut.getMapping(lutX, component, lut.getP2());
                int y1 = (int) Math.round(lutY1 / dx);
                int y2 = (int) Math.round(lutY2 / dx);

                g2.setColor(Color.RED);
                g2.drawLine(lastX, graphSize - lastY1, x, graphSize - y1);
                g2.setColor(Color.GREEN);
                g2.drawLine(lastX, graphSize - lastY2, x, graphSize - y2);
                lastX = x;
                lastY1 = y1;
                lastY2 = y2;
            }


            ct = AffineTransform.getTranslateInstance(-offset.x, -offset.y);
            g2.transform(ct);

            // legend
            g2.setColor(Color.BLACK);
            Font font = new Font("Serif", Font.PLAIN, 12);
            g2.setFont(font);

            //  axes values
            g2.drawString(Integer.toString(lut.getRange()[component]), graphSize + offset.x - 20, graphSize + offset.y + 20);
            g2.drawString(Integer.toString(lut.getRange()[component]), 20, offset.y * 2);

            //  axes name
            g2.drawString(lut.getP1().toString(), (graphSize + offset.x) / 2, graphSize + offset.y + 35);

            AffineTransform tr = AffineTransform.getTranslateInstance(15, graphSize / 2);
            g2.transform(tr);
            AffineTransform rot = AffineTransform.getRotateInstance(Math.PI * -0.5);
            g2.transform(rot);
            g2.drawString(lut.getP2().toString(), 0, 0);
        }
        g2.setTransform(oldat);

    }
}
